// export TF_VAR_alicloud_access_key=
// export TF_VAR_alicloud_secret_key=
// 驱动
terraform {
  required_providers {
    alicloud = {
      source = "aliyun/alicloud"
    }
  }
}

// 声明变量
variable "alicloud_access_key" {
 type = string
}
variable "alicloud_secret_key" {
  type = string
}

// ak sk 可用区
provider "alicloud" {
  access_key = var.alicloud_access_key
  secret_key = var.alicloud_secret_key
  region = "us-east-1"
}

// VPC专有网络
resource "alicloud_vpc" "vpc" {
  vpc_name = "langchain"
  cidr_block = "172.16.0.0/12"
}

// switch交换机
resource "alicloud_vswitch" "vsw" {
  vpc_id = alicloud_vpc.vpc.id
  cidr_block = "172.17.0.0/21"
  zone_id = "us-east-a"
}

// security_group 安全组
resource "alicloud_security_group" "group" {
  name = "langchain"
  vpc_id = alicloud_vpc.vpc.id
  security_group_type = "normal" // 普通类型
}

// security_group_rule 规则
resource "alicloud_security_group_rule" "allow_80_tcp" {
  type = "ingress"
  ip_protocol = "tcp"
  nic_type = "intranet"
  policy = "accept"
  port_range = "80/80"
  priority = 1
  security_group_id = alicloud_security_group.group.id
  cidr_ip = "0.0.0.0/0"
}

// security_group_rule 规则
resource "alicloud_security_group_rule" "allow_22_tcp" {
  type = "ingress"
  ip_protocol = "tcp"
  nic_type = "intranet"
  policy = "accept"
  port_range = "22/22"
  priority = 1
  security_group_id = alicloud_security_group.group.id
  cidr_ip = "0.0.0.0/0"
}

// ecs按量付费
resource "alicloud_instance" "myecs" {
  availability_zone = "us-east-a"
  security_groups = alicloud_security_group.group.*.id
  instance_type = "ecs.xn4.small"
  system_disk_category = "cloud_efficiency"
  system_disk_name = "langchain"
  system_disk_description = "langchain"
  image_id = "centos_7_9_x64_20G_alibase_20201228.vhd"
  instance_name = "langchain"
  vswitch_id = alicloud_vswitch.vsw.id
  internet_max_bandwidth_out = 1
  internet_charge_type = "PayByTraffic"
  password = "root@123"
}